package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCommentMapper;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Writer;
import java.util.List;

/**
 * @author wanghaotian
 * time:2022/12/21,0021 13:04
 * description:
 */

@Service
public class DtsCommentServiceImpl implements DtsCommentService {

    @Autowired
    private DtsCommentMapper commentMapper;

    @Override
    public IPage<DtsComment> findPage(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order, List<Integer> brandIds) {
        IPage<DtsComment> iPage = new Page<>(page,limit);
        QueryWrapper<DtsComment> wrapper = new QueryWrapper<>();
        if (userId != null) {
            wrapper.eq("c.user_id", userId);
        }
        if (valueId != null) {
            wrapper.eq("c.value_id", valueId);
        }
        if (brandIds != null && brandIds.size() > 0) {
            wrapper.in("g.brand_id",brandIds);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("desc")) {
                wrapper.orderByDesc(sort);
            }else {
                wrapper.orderByAsc(sort);
            }
        }
        iPage = commentMapper.selectPage(iPage, wrapper);
        return iPage;
    }

    @Override
    public void delete(DtsComment comment) {
        commentMapper.deleteById(comment);
    }
}
